package leftMain;
//package org.dlsu.mt.parser;

import org.dlsu.mt.DictionaryInterface;
import org.dlsu.mt.MorphInterface;
import org.dlsu.mt.SentenceTokenizerInterface;
import org.dlsu.mt.SimpleMorphAnalyzer;
import org.dlsu.mt.SimpleSentenceTokenizer;
import org.dlsu.mt.parser.FStructure;
import org.dlsu.mt.parser.GLRParserEngine;
import org.dlsu.mt.parser.LexicalAnalyzerInterface;
import org.dlsu.mt.parser.NLPParser;
import org.dlsu.mt.parser.SimpleLexer;
import org.dlsu.mt.parser.generator.GLRParserGenerator;
import org.dlsu.mt.parser.generator.ParseTable;

public class AnalysisModule
{
	String grammarFile = null;
	DictionaryInterface dictio = null;
	GLRParserGenerator glr;
	ParseTable pt;
	SentenceTokenizerInterface senTok;
	LexicalAnalyzerInterface lexAnalyzer;
	GLRParserEngine glrEngine;
	MorphInterface morph;
	NLPParser nlpp;
	
	public AnalysisModule(){
		
	}
	
	public AnalysisModule(String grammar, DictionaryInterface dictio) throws Exception
	{
		grammarFile = grammar;
		this.dictio = dictio;
		init();
	}
		
	public void init() throws Exception
	{
		glr = new GLRParserGenerator(grammarFile);
		pt = glr.createParseTable();
		senTok = new SimpleSentenceTokenizer();
		lexAnalyzer = new SimpleLexer(" ",false);
		glrEngine = new GLRParserEngine();
		morph = new SimpleMorphAnalyzer();
		nlpp = new NLPParser();
		
		nlpp.setLexer(lexAnalyzer);
		nlpp.setParser(glrEngine);
		
		nlpp.setDictionary(dictio);
		
		nlpp.setMorph(morph);
		nlpp.setSentenceTokenizer(senTok);
		nlpp.setParseTable(pt);
	}
	
	public void setDictionary(DictionaryInterface d)
	{
		dictio = d;
		nlpp.setDictionary(dictio);
	}
	
	public void setGrammar(String filename) throws Exception
	{
		grammarFile = filename;
		glr = new GLRParserGenerator(grammarFile);
		pt = glr.createParseTable();
		nlpp.setParseTable(pt);
	}
	
	public void reload(String grammar, DictionaryInterface dictio) throws Exception
	{
		grammarFile = grammar;
		this.dictio = dictio;
		init();
	}
	
	public FStructure analyze(String doc) throws Exception
	{				
		nlpp.setDocument(doc);
		FStructure f = nlpp.analyzeToFStructure();			
		nlpp.saveParseResultToFile("/home/pckid/LEFT/output.xml","/home/pckid/LEFT/sem.xml");	
		return f;
	}
	
	public FStructure analyzeEnglish(String doc) throws Exception
	{				
		nlpp.setDocument(doc);
		FStructure f = nlpp.analyzeToFStructure();	
		//System.out.println("Here " + doc );
		nlpp.saveParseResultToFile("/home/pckid/LEFT/EnglishAnalysis.xml","/home/pckid/LEFT/EnglishSemantics.xml");	
		return f;
	}
	
	public FStructure analyzeFilipino(String doc) throws Exception
	{				
		nlpp.setDocument(doc);
		FStructure f = nlpp.analyzeToFStructure();	
		//System.out.println("Here " + doc );
		nlpp.saveParseResultToFile("/home/pckid/LEFT/FilipinoAnalysis.xml","/home/pckid/LEFT/FilipinoSemantics.xml");	
		return f;
	}
	
	
}